package com.meritdata.cloud.data.standard.table.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.meritdata.cloud.data.base.ResultData;
import com.meritdata.cloud.data.standard.table.dao.IDictPartStructDao;
import com.meritdata.cloud.data.standard.table.dao.IDictPartValueDao;
import com.meritdata.cloud.data.standard.table.entity.TdDictPartStruct;
import com.meritdata.cloud.data.standard.table.service.IDictPartStructService;

@Service
public class DictPartStructService implements IDictPartStructService {

	@Autowired
	private IDictPartStructDao dictPartStructDao;

	@Autowired
	private IDictPartValueDao dictPartValueDao;

	@Override
	public List<TdDictPartStruct> list(Long dictId) {
		return dictPartStructDao.findEqualField("dictId", dictId);
	}

	@Override
	@Transactional
	public ResultData delete(Long id) {
		ResultData result = new ResultData();
		dictPartValueDao.deleteEqualField("partId", id);
		int delete = dictPartStructDao.delete(id);
		result.setStatus(delete > 0);
		return result;
	}

	@Override
	public ResultData create(String name, String code, Long dictId, String userId) {
		ResultData result = new ResultData();

		if (isExistName(name, dictId, 0L)) {
			result.setStatus(false);
			result.setMsg("名称已存在");
			return result;
		}

		if (isExistCode(code, dictId, 0L)) {
			result.setStatus(false);
			result.setMsg("编码已存在");
			return result;
		}

		TdDictPartStruct partStruct = new TdDictPartStruct();
		partStruct.setCode(code);
		partStruct.setName(name);
		partStruct.setDictId(dictId);
		partStruct.setCreateTime(new Date());
		partStruct.setCreator(userId);
		dictPartStructDao.save(partStruct);
		result.setData(partStruct);
		return result;
	}

	@Override
	public ResultData update(String name, String code, Long id, String userId) {
		ResultData result = new ResultData();
		TdDictPartStruct partStruct = dictPartStructDao.findOne(id);
		if (partStruct == null) {
			result.setStatus(false);
			result.setMsg("码表业务已不存在");
			return result;
		}

		if (isExistName(name, partStruct.getDictId(), id)) {
			result.setStatus(false);
			result.setMsg("名称已存在");
			return result;
		}

		if (isExistCode(code, partStruct.getDictId(), id)) {
			result.setStatus(false);
			result.setMsg("编码已存在");
			return result;
		}

		partStruct.setCode(code);
		partStruct.setName(name);
		partStruct.setUpdateTime(new Date());
		partStruct.setReviserId(userId);
		dictPartStructDao.update(partStruct);
		result.setData(partStruct);
		return result;
	}

	private boolean isExistName(String name, Long dictId, Long id) {
		Long countHql = dictPartStructDao
				.countHql("from TdDictPartStruct t where t.dictId = ? and t.name = ? and id <> ?", dictId, name, id);
		return countHql > 0;
	}

	private boolean isExistCode(String code, Long dictId, Long id) {
		Long countHql = dictPartStructDao
				.countHql("from TdDictPartStruct t where t.dictId = ? and t.code = ? and id <> ?", dictId, code, id);
		return countHql > 0;
	}

	@Override
	@Transactional
	public ResultData modify(List<Map<String, Object>> parts, String userId) {
		ResultData result = new ResultData();
		for (Map<String, Object> part : parts) {
			Long id = part.get("id") == null ? null : Long.valueOf(String.valueOf(part.get("id")));
			Long dictId = part.get("dictId") == null ? null : Long.valueOf(String.valueOf(part.get("dictId")));
			String code = (String) part.get("code");
			String name = (String) part.get("name");
			if (id == null) {
				// 新增
				if (dictId == null || StringUtils.isBlank(code) || StringUtils.isBlank(name)) {
					continue;
				}
				TdDictPartStruct partStruct = new TdDictPartStruct();
				partStruct.setCode(code);
				partStruct.setCreateTime(new Date());
				partStruct.setCreator(userId);
				partStruct.setDictId(dictId);
				partStruct.setName(name);
				dictPartStructDao.save(partStruct);
			} else {
				// 更新
				TdDictPartStruct partStruct = dictPartStructDao.findOne(id);
				if (partStruct == null) {
					continue;
				}
				partStruct.setCode(code);
				partStruct.setName(name);
				partStruct.setReviserId(userId);
				partStruct.setUpdateTime(new Date());
				dictPartStructDao.update(partStruct);
			}

		}
		return result;
	}

}
